package com.atguigu.gamll1021.gmall1021_publisher.controller;

import com.atguigu.gamll1021.gmall1021_publisher.bean.ProvinceStats;
import com.atguigu.gamll1021.gmall1021_publisher.server.ProvinceStatsService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/sugar")
public class SugarController {
    @Autowired
    ProvinceStatsService provinceStatsService;

    @RequestMapping("/province")
    public String getProvinceStats(
            @RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) date = now();

        List<ProvinceStats> provinceStatsList = provinceStatsService.getProvinceStats(date);
        StringBuilder builder = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceStatsList.size(); i++) {
            ProvinceStats provinceStats = provinceStatsList.get(i);
            builder.append("{\"name\": \"" + provinceStats.getProvince_name() + "\",\"value\": " + provinceStats.getOrder_amount() + "}");
            if (i < provinceStatsList.size() - 1) {
                builder.append(",");
            }
        }

        builder.append("],\"valueName\": \"交易额\"}}");
        return builder.toString();

        //将字符串转换为 202151格式,将现在的时间赋值给date


    }

    //获取当前日期
    private Integer now() {
        String yyyyMMdd = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.valueOf(yyyyMMdd);
    }
}